#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tornado
import tornado.web
try:
    import ujson as json
except:
    import json
from controls import data_user_brief_mysql
from controls import data_user_detail_mysql
from controls import data_img_base_mysql
from controls import data_fake_user_mysql
from controls import mongo
from utils import log
from utils.file_util import file_upload
from config import etc

class BaseHandler(tornado.web.RequestHandler):
    def get_current_user(self):
        return self.get_secure_cookie("username")

class AddFakeUserHandler(BaseHandler):
    def post(self, *args, **kwargs):
        name = self.get_argument('name')
        sex = self.get_argument('sex')
        sexot = self.get_argument('sexot')
        love = self.get_argument('love')
        horo = self.get_argument('horo')
        intro = self.get_argument('intro')

        userid = mongo.count_userid()

        if not (name and sex and sexot and love and horo and intro):
            log.i('params fetching error')
            self.redirect('/cms/manageuser')
            return

        file = self.request.files['file'][0]
        file_res = file_upload(file)

        if not (file_res and file_res['ret'] == 0):
            res = {'ret': 1, 'msg': '保存图片失败'}
            self.write(json.dumps(res))
            self.finish()
            return

        imglink = data_img_base_mysql.get_img_url_by_imgid(file_res['res']['imgid'])

        fake_user_item = {'userid': userid}

        detail_item = {
            'userid': userid,
            'sex': sex,
            'sexot': sexot,
            'love': love,
            'horo': horo
        }

        brief_item = {
            'userid': userid,
            'name': name,
            'intro': intro,
            'zone': name + u'的空间',
            'imglink': imglink
        }

        fake_user = data_fake_user_mysql.add_fake_user_item(fake_user_item)
        detail = data_user_detail_mysql.add_user_detail_item(detail_item)
        brief = data_user_brief_mysql.add_user_brief_item(brief_item)

        if fake_user and detail and brief:
            res = {'ret': 0, 'msg': '添加用户成功'}
            self.write(json.dumps(res))
            self.finish()
            return
        else:
            res = {'ret': 1, 'msg': '添加用户失败'}
            self.write(json.dumps(res))
            self.finish()
            return

class GetAllFakeUsersHandler(BaseHandler):
    def post(self, *args, **kwargs):
        begin = self.get_argument('begin')
        limit = self.get_argument('limit')

        if not (begin and limit):
            log.i('params fetching error')
            res = {'ret': 1, 'msg': '获取参数错误'}
            self.write(json.dumps(res))
            self.finish()
            return

        fake_userid_list = data_fake_user_mysql.get_fake_user_all(begin, limit)
        fake_user_brief = data_user_brief_mysql.get_user_brief_by_userid_list(fake_userid_list)
        if fake_user_brief:
            res = {'ret': 0, 'msg': 'ok', 'res': fake_user_brief}
            self.write(json.dumps(res))
            self.finish()
            return
        else:
            res = {'ret': 1, 'msg': '获取用户失败'}
            self.write(json.dumps(res))
            self.finish()
            return

class UpdateOneFakeUserHandler(BaseHandler):
    def get(self, *args, **kwargs):
        userid = self.get_argument('userid')
        fake_user_brief = data_user_brief_mysql.get_user_brief_by_userid(userid)
        fake_user_detail = data_user_detail_mysql.get_user_detail_by_userid(userid)

        item = {'userid': fake_user_brief['userid'],
                'name': fake_user_brief['name'],
                'intro': fake_user_brief['intro'],
                'zone': fake_user_brief['zone'],
                'imglink': fake_user_brief['imglink'],
                'sex': fake_user_detail['sex'],
                'sexot': fake_user_detail['sexot'],
                'love': fake_user_detail['love'],
                'horo': fake_user_detail['horo']
        }

        if item:
            res = {'ret': 0, 'msg': 'ok', 'res': item}
            self.write(json.dumps(res))
            self.finish()
            return
        else:
            res = {'ret': 1, 'msg': '获取用户失败'}
            self.write(json.dumps(res))
            self.finish()
            return

    def post(self, *args, **kwargs):
        userid = self.get_argument('userid')
        name = self.get_argument('name')
        intro = self.get_argument('intro')
        sex = self.get_argument('sex')
        sexot = self.get_argument('sexot')
        love = self.get_argument('love')
        horo = self.get_argument('horo')

        if not (userid and name and sex and sexot and love and horo and intro):
            log.i('params fetching error')
            self.redirect('/cms/manageuser')
            return

        file = self.request.files['file'][0]
        file_res = file_upload(file)

        if not (file_res and file_res['ret'] == 0):
            res = {'ret': 1, 'msg': '保存图片失败'}
            self.write(json.dumps(res))
            self.finish()
            return

        imglink = data_img_base_mysql.get_img_url_by_imgid(file_res['res']['imgid'])

        detail_item = {
            'userid': userid,
            'sex': sex,
            'sexot': sexot,
            'love': love,
            'horo': horo
        }

        brief_item = {
            'userid': userid,
            'name': name,
            'intro': intro,
            'imglink': imglink
        }

        detail = data_user_detail_mysql.update_fake_user_detail_by_userid(userid, detail_item)
        brief = data_user_brief_mysql.update_fake_user_brief_by_userid(userid, brief_item)

        if detail and brief:
            res = {'ret': 0, 'msg': '用户资料更新成功'}
            self.write(json.dumps(res))
            self.finish()
            return
        else:
            res = {'ret': 1, 'msg': '用户资料更新失败'}
            self.write(json.dumps(res))
            self.finish()
            return